package com.liuxin.mycontactdemo.adapter;


import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;


import com.liuxin.mycontactdemo.R;
import com.liuxin.mycontactdemo.vo.Contact;

import java.util.List;

public class ExportAdapter extends BaseAdapter {
    private Context context;
    private List<Contact> contextData;


    public ExportAdapter(Context context, List<Contact> contextData) {
        this.context = context;
        this.contextData = contextData;
    }

    @Override
    public int getCount() {
        return this.contextData.size();
    }

    @Override
    public Object getItem(int position) {
        return this.contextData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.export_datalist, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.exportNameTextView = convertView.findViewById(R.id.nameTv_export);
            viewHolder.exportPhoneTextView = convertView.findViewById(R.id.phoneTv_export);
            viewHolder.exportEmailTextView = convertView.findViewById(R.id.emailTv_export);
            viewHolder.exportCheckBox = convertView.findViewById(R.id.exportCb);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //设置数据
        viewHolder.exportNameTextView.setText(contextData.get(position).getName());
        viewHolder.exportPhoneTextView.setText(contextData.get(position).getPhone());
        viewHolder.exportEmailTextView.setText(contextData.get(position).getEmail());
        if(contextData.get(position).isChecked()){
            viewHolder.exportCheckBox.setChecked(true);
        }else{
            viewHolder.exportCheckBox.setChecked(false);
        }
        viewHolder.exportCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewHolder.exportCheckBox.isChecked()){
                    contextData.get(position).setChecked(true);

                }else {
                    contextData.get(position).setChecked(false);
                }
            }
        });
        return convertView;
    }
    class ViewHolder{
        TextView exportNameTextView,exportPhoneTextView,exportEmailTextView;
        CheckBox exportCheckBox;
    }
}